package com.lg.atp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.lg.atp.common.Constants;
import com.lg.atp.entity.TblAppInfo;
import com.lg.atp.entity.TblTestTask;
import com.lg.atp.entity.TblTestTaskCase;
import com.lg.atp.entity.TblTestTaskDevice;
import com.lg.atp.sercurity.SecurityUserHolder;
import com.lg.atp.service.IAppInfoService;
import com.lg.atp.service.ITaskService;

@RestController
@RequestMapping("task")
public class TestTaskController {

	private static Log log = LogFactory.getLog(TestTaskController.class);
	
	@Autowired
	private ITaskService iTaskService;
	@Autowired
	private IAppInfoService iAppInfoService;
	
	@GetMapping("index")
	public ModelAndView toIndex() {
		ModelAndView view = new ModelAndView();
		view.setViewName("taskList");
		return view;
	}
	
	@GetMapping("toAdd")
	public ModelAndView toAdd() {
		ModelAndView view = new ModelAndView();
		view.setViewName("task-add");
		List<TblAppInfo> applist = iAppInfoService.selectEditedApp();
		view.addObject("applist", applist);
		return view;
	}
	
	
	@GetMapping("toEdit")
	public ModelAndView toEdit(Long taskId) {
		ModelAndView view = new ModelAndView();
		view.setViewName("task-edit");
		view.addObject("taskId", taskId);
		List<TblAppInfo> applist = iAppInfoService.selectEditedApp();
		view.addObject("applist", applist);
		return view;
	}
	
	@GetMapping("toView")
	public ModelAndView toView(Long taskId) {
		ModelAndView view = new ModelAndView();
		view.setViewName("task-view");
		view.addObject("taskId", taskId);
		List<TblAppInfo> applist = iAppInfoService.selectEditedApp();
		view.addObject("applist", applist);
		return view;
	}

	@GetMapping("toCaseSelect")
	public ModelAndView toCaseSelect(String caseIds) {
		ModelAndView view = new ModelAndView();
		view.setViewName("case-sel");
		view.addObject("caseIds", caseIds);
		List<TblAppInfo> applist = iAppInfoService.selectEditedApp();
		view.addObject("applist", applist);
		return view;
	}
	
	@GetMapping("toDeviceSelect")
	public ModelAndView toDeviceSelect(String deviceIds) {
		ModelAndView view = new ModelAndView();
		view.setViewName("device-sel");
		view.addObject("deviceIds", deviceIds);
		return view;
	}
	
	
	
	@PostMapping("list")
	public Map<String,Object> getTaskList(String taskName,Integer pageIndex,Integer pageSize){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("pageIndex",pageIndex);
			map.put("pageSize", pageSize);
			TblTestTask task = new TblTestTask();
			task.setTaskName(taskName);
			PageInfo<TblTestTask> list = iTaskService.selectAllTestTask(map);
			result.put("data", list);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("获取任务列表失败："+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "获取任务列表失败");
		}
		return result;
	}
	//getTaskById
	
	@PostMapping("getTaskById")
	public Map<String,Object> getTaskById(Long taskId){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
             TblTestTask task = iTaskService.getTaskById(taskId);
             List<TblTestTaskCase> caselist = iTaskService.selectCaseFromTask(taskId);
             List<TblTestTaskDevice> devicelist = iTaskService.selectDeviceFromTask(taskId);
             
             result.put("data", task);
             result.put("casedata", caselist);
             result.put("devicedata", devicelist);
			
		}catch(Exception e) {
			e.printStackTrace();
			log.error("获取任务失败："+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "获取任务失败");
		}
		return result;
	}
	
	@PostMapping("saveTask")
	public Map<String,Object> saveTask(TblTestTask task){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			if(StringUtils.isNotBlank(task.getPlanEndDateStr()) && StringUtils.isNotBlank(task.getPlanStartDateStr())) {
				task.setPlanStartDate(DateUtils.parseDate(task.getPlanStartDateStr().trim(), Constants.pattern));
				task.setPlanEndDate(DateUtils.parseDate(task.getPlanEndDateStr().trim(), Constants.pattern));
			}
			task.setCreateUserId(SecurityUserHolder.getCurrentUser().getId());
			iTaskService.saveTestTask(task);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("保存任务失败："+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "保存任务失败");
		}
		return result;
	}


	@PostMapping("updateTask")
	public Map<String,Object> updateTask(TblTestTask task){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			if(StringUtils.isNotBlank(task.getPlanEndDateStr()) && StringUtils.isNotBlank(task.getPlanStartDateStr())) {
				task.setPlanStartDate(DateUtils.parseDate(task.getPlanStartDateStr().trim(), Constants.pattern));
				task.setPlanEndDate(DateUtils.parseDate(task.getPlanEndDateStr().trim(), Constants.pattern));
			}
			iTaskService.updateTestTask(task);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("保存任务失败："+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "保存任务失败");
		}
		return result;
	}
	
}
